package com.dcy.system.service;

import com.dcy.common.base.service.DcyBaseService;
import com.dcy.common.utils.TreeUtil;
import com.dcy.system.dao.DeptDao;
import com.dcy.system.vo.in.DeptCreateInVO;
import com.dcy.system.vo.in.DeptSearchInVO;
import com.dcy.system.vo.in.DeptUpdateInVO;
import com.dcy.system.vo.out.DeptListOutVO;
import com.dcy.system.convert.DeptConvert;
import com.dcy.system.model.Dept;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 部门表 服务实现类
 * </p>
 *
 * @author dcy
 * @since 2021-03-16
 */
@RequiredArgsConstructor
@Service
public class DeptService extends DcyBaseService {

    private final DeptDao deptDao;
    private final DeptConvert deptConvert = DeptConvert.INSTANCE;

    /**
     * 获取部门的tree数据
     *
     * @param deptSearchInVO
     * @return
     */
    public List<DeptListOutVO> getDeptTreeList(DeptSearchInVO deptSearchInVO) {
        return deptConvert.toOutList(TreeUtil.listToTree(deptDao.listByEntity(deptConvert.toDept(deptSearchInVO)), Comparator.comparing(Dept::getDeptSort)));
    }

    /**
     * 保存
     *
     * @param deptCreateInVO
     * @return
     */
    public Boolean save(DeptCreateInVO deptCreateInVO) {
        return deptDao.save(deptConvert.toDept(deptCreateInVO));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public Boolean delete(String id) {
        return deptDao.removeById(id);
    }

    /**
     * 修改
     *
     * @param deptUpdateInVO
     * @return
     */
    public Boolean update(DeptUpdateInVO deptUpdateInVO) {
        return deptDao.updateById(deptConvert.toDept(deptUpdateInVO));
    }
}
